rm rootfs_ext4_initramfs_switch_root.img
dd if=/dev/zero of=rootfs_ext4_initramfs_switch_root.img bs=1M count=20
mkfs.ext4 rootfs_ext4_initramfs_switch_root.img
mkdir -p tmpfs
mount -t ext4 rootfs_ext4_initramfs_switch_root.img tmpfs/ -o loop
cp -af _install/* tmpfs/
touch tmpfs/hello.txt
echo test-test-switch_root > tmpfs/hello.txt

cat > tmpfs/init <<EOF
#!/bin/sh
echo "Welcome to linux... $(date "+%Y%m%d%H%M%S")"
echo "current pid \$\$"

echo "exec initramfs init"
echo "mounting proc and sys"
 
mount -t proc proc /proc
mount -t sysfs sysfs /sys
 
echo "detect and export hardware info"
mdev -s
 
echo "Mount real rootfs to /mnt ..."
mount -t ext4 /dev/vda /mnt
 
echo "Switch to real rootfs..."
 
exec switch_root /mnt /sbin/init

EOF

chmod +x tmpfs/init
cat tmpfs/init

umount tmpfs
rm -rf tmpfs
chmod 777 rootfs_ext4_initramfs_switch_root.img
